using System;
using S2Engine.Game;
using System.Collections.Generic;

namespace S2Engine.Timeline
{
    public class SceneManager
    {
        #region Attributes
        //private List<IScene> scenes = new List<IScene>();
        private Dictionary<string, IScene> scenes = new Dictionary<string, IScene>();
        private IScene currentscene;
        private S2Game game;
        #endregion

        #region Ctor
        public SceneManager(S2Game game)
        {
            this.game = game;
            //TODO: Load ScenesFile
            //ReadXmlFile(Application.StartupPath + "\\..\\..\\Scenes\\SceneManager.xml");
            //currentscene = scenes[0];
        }
        #endregion

        #region Properties
        public S2Game Game
        {
            get { return game; }
        }

        public IScene CurrentScene
        {
            get { return currentscene; }
            set {
                lock (value){
                    if (currentscene != null)
                    {
                        currentscene.Stop();
                        currentscene.Dispose();
                    }
                
                currentscene = value;
                if (!currentscene.Ready)
                    currentscene.Load();
                currentscene.Start();
            }
            }
        }

        public int CurrentSceneIndex
        {
            get { return currentscene.Index; }
            set {
                if (value < 0)
                    value = 0;

                if (value > scenes.Count-1)
                    value = scenes.Count-1;
            
                    if (currentscene != null)
                        currentscene.Stop();
                    //CurrentScene = this.Scenes.Values[value];
                    currentscene.Start();  
            }
        }

        public Dictionary<string,IScene> Scenes
        {
            get { return scenes; }
            set { scenes = value; }
        }
        #endregion

        #region Methods
        public void Next()
        {
            Foward(1);
        }

        public void Previous()
        {
            Rewind(1);
        }

        public void Foward(int qtd)
        {
            CurrentSceneIndex += qtd;
        }

        public void Rewind(int qtd)
        {
            CurrentSceneIndex -= qtd;
        }
        
        #endregion
    }
}
